package com.g2.mydysingle.controller;

import com.g2.mydysingle.common.Result;
import com.g2.mydysingle.model.DlMovieInfo;
import com.g2.mydysingle.service.IDlMovieInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

/**
 * <p>
 * 电影信息表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-10-15
 */
@RestController
@RequestMapping("/mydysingle/dlMovieInfo")
@Slf4j  //日志打印
@Api(tags = "电影信息接口")
public class DlMovieInfoController {


    @Autowired
    private IDlMovieInfoService iDlMovieInfoService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping
    @ApiOperation("查询全部")
    public Result getList(){
        log.info("-------查询全部-------");
        List<DlMovieInfo> list = iDlMovieInfoService.list();
        return Result.ok(list);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过id查询")
    public ModelAndView getById(@ApiParam(value = "id") @PathVariable Integer id){
        DlMovieInfo model = iDlMovieInfoService.getById(id);

        //ModelAndView 使用这个来做页面跳转
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/ljx/ljx_dyxqy");
        mv.addObject("model",model);

        return mv;
    }



    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("通过id删除")
    public Result deleteBuId(@ApiParam(value = "id") @PathVariable Integer id){
        boolean b = iDlMovieInfoService.removeById(id);
        if (b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    /**
     * 插入数据
     * @param model
     * @return
     */
    @PostMapping
    @ApiOperation("插入数据")
    public Result insert(@ApiParam(name = "电影信息对象",value = "传入json格式") @RequestBody DlMovieInfo model){
        boolean b = iDlMovieInfoService.save(model);
        if (b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    /**
     * 通过id修改
     * @param model
     * @return
     */
    @PutMapping
    @ApiOperation("通过id修改数据")
    public Result update(@ApiParam(name = "电影信息对象",value = "传入json格式") @RequestBody DlMovieInfo model){
        boolean b = iDlMovieInfoService.updateById(model);
        if (b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }


}
